package com.baobao.action;


import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import com.baobao.domain.Course;
import com.baobao.domain.Student;
import com.baobao.service.CourseService;
import com.baobao.service.StudentService;


@Controller
@RequestMapping("/course")
public class CourseAction {

	private static Logger log = LoggerFactory.getLogger(LeaveAction.class);
	
	@Autowired
	private CourseService courseService;
	
	@Autowired
	private StudentService studentService;
	
	/**
	 * 获取课程列表
	 */
	@RequestMapping(value="/get",  produces = "application/json; charset=utf-8",method = {RequestMethod.GET})
	@ResponseBody
	public String GetCourse(HttpServletRequest request,
		@RequestParam(value="id",required=true) String id){
		
		JSONObject result = new JSONObject();
		
		//根据BBID找到该学生所在班级
		Student student = studentService.findStudentByPrimaryKey(id);
		String banjiId = student.getClassId();
		
		JSONArray jsonArray = new JSONArray();
		JSONArray jsonArray_week_course = new JSONArray();
		
		for(int week_day = 0;week_day <=6;week_day++)
		{
			jsonArray.clear();
			//根据班级、星期几查询当天的课程列表
			for(int seqNo = 1;seqNo <=8; seqNo ++)
			{
				Course course = courseService.findCourseByPrimaryKey(banjiId, week_day, seqNo);
				if(course == null)
				{
					course = new Course();
					course.setName("--");
					course.setSeqNo(seqNo);
					course.setStartTime("--");
					course.setEndTime("--");
				}
				jsonArray.add(course);			
			}
			jsonArray_week_course.add(jsonArray);
		}
		
		
		result.put("success",true);
		result.put("message", jsonArray_week_course);
		
		log.info("响应客户端查询课程列表："+result.toString());
		return result.toString();
		
		
		
	}
	
}
